/*
DESCRIPTION
This do file creates the tables in the online-only appendix.

INPUT
	data/BITTrialAdoption_Identified.dta // With identifiable city characteristics, not in public file
	data/BITTrialAdoption.dta
	data/NudgeUnits.dta // from https://doi.org/10.3982/ECTA18709
	data/NewBITTrials.dta
	data/policyadoption_raw.csv // Hjort et al. (2021) data from http://doi.org/10.3886/E122661V1 
	data/policyexperiment_municIDs_char.dta // Confidential Hjort et al. (2021) data with merged municipalities and mayors 
	
OUTPUT
	tables/appendix/cityfeatures_balance.tex // Will not run - requires identifiable city-level data
	tables/appendix/averageeffect.tex
	tables/appendix/sumstats_city.tex // Will not run - requires identifiable city-level data
	tables/appendix/sumstats_forecasts.tex
	tables/appendix/recommendations.tex
	tables/appendix/reg_robust.tex
	tables/appendix/sumstats_survey.tex
	tables/appendix/hjort_rates.tex
	tables/appendix/hjort_balance.tex
	tables/appendix/hjort_balance.tex // Will not run - requires merged confidential Hjort et al. (2021) data
*/

****************************************************************
***TABLE A.1: CITY FEATURES OF COMPLETED VS. ABANDONED TRIALS***
****************************************************************
{ // REQUIRES IDENTIFIABLE CITY-LEVEL DATA
	/*
	clear all
	use "data/BITTrialAdoption_Identified.dta"

	gen byte trialcomplete = 1 - abandoned
	replace trialcomplete = 1 if abandoned==.
	gen logpop=log(popestimate2020)
	replace emprate=emprate*100
	replace tax_rev_pc = tax_rev_pc*1000
	replace comm_preexisted=comm_preexisted*100

	local varlist logpop medage medinc medprop emprate whitepct tax_rev_pc WWC_cert comm_preexisted
	local varlab `" "Log(pop)" "Median age of residents" "Median household income" "Median property value" "Employed percent" "White percent" "Tax revenue per capita" "What Works Cities certification" "Trial pre-existed" "'
	local varnum: word count `varlist'

	mata
		balance=J(`varnum'+2,5,"")
		balance[1,2]="Completed trials"
		balance[1,3]="Abandoned trials"
		balance[1,4]="Difference (SE)"
		balance[1,5]="Diff. $ p\text{-value}$"
	end

	local v=1
	foreach var in `varlist' {
		local lab: word `v' of `varlab'
		mata: balance[`v'+1,1]="`lab'"
		
		qui reg `var' ibn.trialcomplete, cl(cityid) nocons
		local x=string(_b[1.trialcomplete],"%12.2f")
		mata: balance[`v'+1,2]="`x'"
		local x=string(_b[0.trialcomplete],"%12.2f")
		mata: balance[`v'+1,3]="`x'"
		qui lincom 1.trialcomplete-0.trialcomplete
		local x=string(`r(estimate)',"%12.2f")
		local y=string(`r(se)',"%12.2f")
		mata: balance[`v'+1,4]="`x' (`y')"
		local x=string(`r(p)',"%3.2f")
		mata: balance[`v'+1,5]="`x'"
		
		local ++v
	}

	qui count if trialcomplete==1
	local x=`r(N)'
	qui distinct cityid if trialcomplete==1
	local y=`r(ndistinct)'
	mata: balance[`varnum'+2,2]="`x' trials (`y' cities)"
	qui count if trialcomplete==0
	local x=`r(N)'
	qui distinct cityid if trialcomplete==0
	local y=`r(ndistinct)'
	mata: balance[`varnum'+2,3]="`x' trials (`y' cities)"
	local line=`varnum'+2
	
	egen trialcompleterate_withincity = mean(trialcomplete), by(cityid)
	qui distinct cityid if trialcompleterate_withincity==0
	loca newcities=`r(ndistinct)'

	mmat2tex balance using "tables/appendix/cityfeatures_balance.tex", /// 
			replace ///
			preheader(\begin{table}[htbp!] \centering \begin{threeparttable} ///
			\caption{City features of completed vs. abandoned trials} \begin{tabular} {l c c c c} \toprule ///
				) ///
			insertendrow(2 "\hline" `line' "\hline") ///
			bottom(\bottomrule \end{tabular} ///
			\label{tab:cityfeatures_balance} ///
			\begin{tablenotes} \footnotesize ///
			\item Standard errors are clustered by city. ///
					There are `newcities' cities in the Abandoned trials sample ///
					that are not in the Completed trials sample. ///
					The remaining cities in the Abandoned trials sample are also ///
					represented in the Completed trials sample. ///
			\end{tablenotes} \end{threeparttable} \end{table})
	*/
}

************************************************
***TABLE A.2: AVERAGE NUDGE TREATMENT EFFECTS***
************************************************
{
	clear all
	use "data/NudgeUnits.dta"
	
	eststo r1: qui reg treatmenteffect, cl(trialnumber)
	qui sum treatmenteffect, d
	estadd scalar q25 `r(p25)'
	estadd scalar median `r(p50)'
	estadd scalar q75 `r(p75)'
	preserve
		egen minsplit = min(trialsplit), by(trialnumber trialsample)
		drop if trialsplit!=minsplit
		collapse (mean) controlN (sum) treatmentN, by(trialnumber trialsample)
		gen N = controlN+treatmentN
		qui sum N
	restore
	estadd local obs = string(round(`r(sum)'),"%12.0gc")
	qui sum controltakeup
	estadd scalar c `r(mean)'
	
	append using "data/NewBITTrials.dta", force
	drop if OES==1
	replace trialnumber=2018017 if trialnumber==2018017.2
	merge m:1 trialnumber using "data/BITTrialAdoption.dta", keepusing(sample) nogen
	keep if sample==1

	eststo r2: qui reg treatmenteffect, cl(trialnumber)
	qui sum treatmenteffect, d
	estadd scalar q25 `r(p25)'
	estadd scalar median `r(p50)'
	estadd scalar q75 `r(p75)'
	preserve
		egen minsplit = min(trialsplit), by(trialnumber trialsample)
		drop if trialsplit!=minsplit
		collapse (mean) controlN (sum) treatmentN, by(trialnumber trialsample)
		gen N = controlN+treatmentN
		qui sum N
	restore
	estadd local obs = string(round(`r(sum)'),"%12.0gc")
	qui sum controltakeup
	estadd scalar c `r(mean)'
	
	esttab r* using "tables/appendix/averageeffect.tex", ///
			se nostar nonotes ///
			mlabels(none) nonumbers ///
			stat(N N_clust obs c header q25 median q75, fmt(0 0 0 %4.2f 0 %4.2f %4.2f %4.2f) ///
			label("Nudges" "Trials" "Observations" "Average control group take-up (\%)" "\textit{Distribution of treatment effects}" "\hspace{1em}25th percentile" "\hspace{1em}50th percentile" ///
			"\hspace{1em}75th percentile"))  ///
			coeflabels(_cons "Average treatment effect") ///
			gaps replace ///
			prehead(\begin{table}[htbp!] \centering \begin{threeparttable} ///
				\caption{Average nudge treatment effects} ///
				 \begin{tabular} {l c c} \toprule ///
				 & Nudge Units* & Updated BIT-NA \\ & (1) & (2) \\) ///
			postfoot(\bottomrule \end{tabular} \label{averageeffect} ///
				\begin{tablenotes} \footnotesize ///
				\item This table shows the average treatment effect of nudges. Standard errors clustered by trial are shown in parentheses. ///
					pp. refers to percentage point. ///
				\item *Column 1 replicates Column 2 of Table III in DellaVigna and Linos (2022). ///
				\end{tablenotes} \end{threeparttable} \end{table})
}

*************************************
***TABLE A.3: CITY CHARACTERISTICS***
*************************************
{ // REQUIRES IDENTIFIABLE CITY-LEVEL DATA
	/*
	clear all 
	use "data/BITTrialAdoption_Identified.dta"
	keep if sample==1

	gen logpop=log(popestimate2020)
	gen emppct=emprate*100
	replace tax_rev_pc = tax_rev_pc*1000

	local varlist 	logpop medage medinc medprop emppct whitepct tax_rev_pc
	local varlab `" "Log(pop)" "Median age of residents" "Median household income" "Median property value" "Employed percent" "White non-Hispanic percent" "Tax revenue per capita" "'
		
	local insertendrow 	1 "\multicolumn{8}{l}{\textit{City characteristics}} \\ %"
	local rows: word count `varlist'
	local rowsplus1 = `rows'+1
	local insertendrow 	`insertendrow' `rowsplus1' "\hline"				
	mata: sumstats=J(`rowsplus1',8,"")

	local r=1
	foreach lab in `varlab' {
		mata: sumstats[`r',1]="\hspace{1em}`lab'"
		local ++r
	}

	qui count if trialnumber!=.
	mata: sumstats[`rows'+1,2]="`r(N)'"

	local catlist treatmenteffect_abovemed staff_same_dpt comm_preexisted
	local r=1
	foreach var in `varlist' {
		qui sum `var'
		local mean=string(`r(mean)',"%10.2f")
		mata: sumstats[`r',2]="`mean'"
		
		local col=3
		foreach cat in `catlist' {
			forvalues x=0/1 {
				qui count if `cat'==`x'
				mata: sumstats[`rowsplus1',`col']="`r(N)'"
			
				qui sum `var' if `cat'==`x'
				local mean=string(`r(mean)',"%10.2f")
				mata: sumstats[`r',`col']="`mean'"
				
				if `x'==1 {
					qui reg `var' 0.`cat', cl(cityid)
					local pval=2*ttail(e(df_r),abs(_b[0.`cat']/_se[0.`cat']))
					if `pval'<0.05 {
						mata: sumstats[`r',`col']="`mean'*"
					}
				}
				local ++col
			}
		}
		
		local ++r
	}

	mmat2tex sumstats using "tables/appendix/sumstats_city.tex", /// 
			replace ///
			preheader( ///
				\begin{table}[htbp!] \centering \begin{threeparttable} ///
				\caption{Sample characteristics: City features} ///
				\begin{tabular} {l c c c c c c c} ///
				\toprule ///
				& Overall & \multicolumn{2}{c}{Effect size{$>$}median} & \multicolumn{2}{c}{City staff retained} & \multicolumn{2}{c}{Comm. pre-existed} \\ ///
				\cmidrule(lr){2-2} \cmidrule(lr){3-4} \cmidrule(lr){5-6} \cmidrule(lr){7-8} ///
				Frequency in category (\%) & (1) & (2) No & (3) Yes & (4) No & (5) Yes & (6) No & (7) Yes \\ ///
				\hline ///
			) ///
			insertendrow(`insertendrow') ///
			bottom(\bottomrule \end{tabular} ///
				\label{tab:sumstats_city} ///
				\begin{tablenotes} \footnotesize ///
					\item This table reports the average city-level features for trials in the sample. Column 1 shows the sample averages. Columns 2 and 3 partition the sample along the median of the maximum effect size in each trial. Columns 4 and 5 consider separately trials for which all the city collaborators from the trial have departed versus trials that have at least one original staff member still working in the same city department. Columns 6 and 7 distinguish between trials that tested nudges in a new communication and those that added nudges to a pre-existing communication that the city had been sending before the trial. ///
					\item *Asterisk indicates that the $ p\text{-value}$ of the difference{$<$}0.05. Standard errors are clustered by city. ///
				\end{tablenotes} \end{threeparttable} \end{table} ///
			)
	*/
}

**********************************
***TABLE A.4: FORECASTS SUMMARY***
**********************************
{
	clear all
	use "data/Forecasts.dta"
	append using "data/BITTrialAdoption.dta"
	drop if abandoned==1
	
	local varlist adopt_bysig adopt_byte adopt_bystaff adopt_bysize adopt_byWWC adopt_bypreexist adopt_bymech
	local lablist `" "\hspace{1em}Positive \& significant" "\hspace{1em}Positive \& insignificant" "\hspace{1em}Zero or negative" "\hspace{1em}High third" "\hspace{1em}Middle third" "\hspace{1em}Low third" "\hspace{1em}With original staff" "\hspace{1em}Without original staff" "\hspace{1em}Above median" "\hspace{1em}Below median" "\hspace{1em}Certified" "\hspace{1em}Not certified" "\hspace{1em}New" "\hspace{1em}Pre-existing" "\hspace{1em}Simplification" "\hspace{1em}Personal motivaton" "\hspace{1em}Social cues" "'
	local lengthlist 3 3 2 2 2 2 3
	local headerlist `" "By sign and significance" "By effect size" "By staff retention" "By state capacity (proxied by 2020 city population size)" "By What Works Cities certification" "By pre-existing or new communication" "By behavioral mechanism" "" "'
	local condlist `" "treatmenteffect_maxt>=invnorm(0.975) & treatmenteffect_maxt!=." "treatmenteffect_maxt>0 & treatmenteffect_maxt<invnorm(0.975) & treatmenteffect_maxt!=." "treatmenteffect_maxt<=0 & treatmenteffect_maxt!=." "treatmenteffect_max>1.7 & treatmenteffect_max!=." "treatmenteffect_max>0.19 & treatmenteffect_max<=1.7 & treatmenteffect_max!=." "treatmenteffect_max<=0.19 & treatmenteffect_max!=." "staff_same_dpt==1" "staff_same_dpt==0" "pop_abovemed==1" "pop_abovemed==0" "WWC_cert==1" "WWC_cert==0" "comm_preexisted==0" "comm_preexisted==1" "mech_simplification==1" "mech_personalmotivation==1" "mech_socialcues==1" "'

	local rows: word count `lablist'
	local rows=`rows'+3
	mata: sum=J(`rows',6,"")
	mata: sum[2,2]="(\%)"
	mata: sum[2,1]="Category"

	qui count if adopt_baseline!=.
	local N=`r(N)'
	mata: sum[1,3]="(1) Overall"
	mata: sum[2,3]="\footnotesize{($ N=`N'$)}"

	qui count if forecaster_NU==1
	local N=`r(N)'
	mata: sum[1,4]="(2) Nudge unit staff"
	mata: sum[2,4]="\footnotesize{($ N=`N'$)}"

	qui count if forecaster_Res==1
	local N=`r(N)'
	mata: sum[1,5]="(3) Reseachers"
	mata: sum[2,5]="\footnotesize{($ N=`N'$)}"

	qui count if forecaster_Gov==1
	local N=`r(N)'
	mata: sum[1,6]="(4) Government workers"
	mata: sum[2,6]="\footnotesize{($ N=`N'$)}"

	mata: sum[3,1]="Baseline adoption rate"
	qui sum adopted if adopted !=.
	local mean=string(`r(mean)'*100,"%4.2f")
	mata: sum[3,2]="`mean'"
	qui sum adopt_baseline, d
	local mean=string(`r(mean)',"%4.2f")
	local sd=string(`r(sd)',"%4.2f")
	mata: sum[3,3]="`mean' \footnotesize{[`sd']}"

	gen one=1 if adopt_baseline!=.
	local grouplist `" "one==1" "forecaster_NU==1" "forecaster_Res==1" "forecaster_Gov==1" "'
	local numgroups: word count `grouplist'
	local collist 3 4 5 6

	forvalues g=1/`numgroups' {
		local groupcond: word `g' of `grouplist' 
		local col: word `g' of `collist'
		qui sum adopt_baseline if `groupcond', d
		local mean=string(`r(mean)',"%4.2f")
		local sd=string(`r(sd)',"%4.2f")
		mata: sum[3,`col']="`mean' \footnotesize{[`sd']}"
	}

	local i=1
	local k=1
	local L=4
	local insertendrow 
	foreach var in `varlist' {
		local l: word `i' of `lengthlist'
		local header: word `i' of `headerlist'
		local insertendrow `insertendrow' `L' "\\ \multicolumn{6}{l}{\textit{`header':}} \\"
		local L=`L'+`l'
		forvalues j=1/`l' {
			local lab: word `k' of `lablist'
			local cond: word `k' of `condlist'
			label var `var'_`j' "`lab'"
			mata: sum[`k'+3,1]="`lab'"
			forvalues g=1/`numgroups' {
				local groupcond: word `g' of `grouplist' 
				local col: word `g' of `collist'
				qui sum `var'_`j' if `groupcond', d
				local mean=string(`r(mean)',"%4.2f")
				local sd=string(`r(sd)',"%4.2f")
				mata: sum[`k'+3,`col']="`mean' \footnotesize{[`sd']}"
			}
			
			qui sum adopted if adopted !=. & `cond'
			local mean=string(`r(mean)'*100,"%4.2f")
			mata: sum[`k'+3,2]="`mean'"
			local ++k
		}
			local ++i
	}
	
	mmat2tex sum using "tables/appendix/sumstats_forecasts.tex", /// 
			replace ///
			preheader(\begin{table}[htbp!] \centering \footnotesize \begin{threeparttable} ///
			\caption{Forecasts summary} \begin{tabular} {l c c c c c} \toprule ///
				& Observed & \multicolumn{4}{c}{Forecasts (Mean \% \footnotesize{[SD]})} \\ ///
				\cmidrule(lr){2-2} \cmidrule(lr){3-6} ///
				) ///
			insertendrow(3 "\hline" `insertendrow') ///
			bottom(\bottomrule \end{tabular} ///
			\label{tab:sumstats_forecasts} ///
			\begin{tablenotes} \scriptsize ///
			\item ///
			\end{tablenotes} \end{threeparttable} \end{table})
}

*******************************************************
***TABLE A.5: BIT TRIAL RECOMMENDATIONS FOR ADOPTION***
*******************************************************
{ 
	clear all
	use "data/BITTrialAdoption.dta"
	keep if sample==1
	
	gen byte recommendation_nonadopt=(recommendation_adopt==0)
	replace treatmenteffect_max=treatmenteffect_max/10
	replace recommendation_adopt=0 if recommendation==0
	
	xtset cityid

	eststo clear
	local i=1
	local c

	eststo r`i': reg recommendation_adopt treatmenteffect_maxt_sig treatmenteffect_max if recommendation==1, cl(cityid)
	qui sum recommendation_adopt if e(sample)==1
	estadd local avg=string(`r(mean)',"%4.2f")
	estadd local withrec="$\checkmark$"
	local mlab `mlab' "(`i')"
	local ++i
	local c `c' c

	eststo r`i': reg adopted recommendation_adopt recommendation_nonadopt comm_preexisted, cl(cityid)
	qui sum recommendation_adopt if e(sample)==1
	estadd local avg=string(`r(mean)',"%4.2f")
	estadd local withrec=""
	local mlab `mlab' "(`i')"
	local ++i
	local c `c' c

	esttab r* using "tables/appendix/recommendations.tex", ///
		substitute(\_ _) ///
		label nonumber se nostar nogaps nonotes b(%4.2f) /// 
		mlabels(`mlab', lhs("(OLS)")) ///
		varlabels(	treatmenteffect_maxt_sig "Max $ t\geq1.96$" ///
					treatmenteffect_max "Max treatment effect (10pp.)" ///
					recommendation_adopt "BIT recommended for adoption" ///
					recommendation_nonadopt "BIT did not recommend for adoption" ///
					comm_preexisted "Communication pre-existed" ///
					_cons "Constant" ///
				) ///
		stat(avg withrec N N_clust r2, fmt(%4.2f 0 0 0 %4.2f) label("Average recommendation rate" "Trials with recommendations only" "Number of trials" "Number of cities" "$ R^2$")) ///
		prehead(\begin{table}[htbp!] \centering \begin{threeparttable} ///
				\begin{tabular} {l `c'} \toprule ///
				\caption{BIT recommendations for trial adoption} ///
				Dep. Var.: & BIT recommended adopt & Trial adopted \\ ///
				\cmidrule(lr){2-2} \cmidrule(lr){3-3} ///
				) ///
		postfoot(\bottomrule \end{tabular} \label{tab:recommendations} ///
			\begin{tablenotes} \footnotesize ///
			\item Standard errors clustered by city are shown in parentheses. ///
				BIT has included recommendations for or against adoption in their trial reports since mid-2017. ///
				In Column 2, the omitted group are the earlier trials without BIT recommendations in the trial reports. ///
			\end{tablenotes} \end{threeparttable} \end{table}) ///
		replace
}

************************************************************
***TABLE A.6: DETERMINANTS OF NUDGE ADOPTION (ROBUSTNESS)***
************************************************************
{
	clear all
	use "data/BITTrialAdoption.dta"
	keep if sample==1

	replace treatmenteffect_max=treatmenteffect_max/10

	eststo clear
	local i=1
	local c
	local mlab

	eststo r`i': reg adopted treatmenteffect_maxt_sig treatmenteffect_max staff_same_dpt pop_abovemed WWC_cert comm_preexisted mech_personalmotivation mech_simplification mech_socialcues, cl(cityid)
	qui sum adopted if e(sample)==1
	estadd local avg=string(`r(mean)',"%4.2f")
	estadd local nomarg  
	estadd local noveri 
	local mlab `" `mlab' "(`i') Baseline" "'
	local ++i
	local c `c' c

	eststo r`i': reg adopted treatmenteffect_maxt_sig treatmenteffect_max staff_same_dpt pop_abovemed WWC_cert comm_preexisted mech_personalmotivation mech_simplification mech_socialcues, r
	qui sum adopted if e(sample)==1
	estadd local avg=string(`r(mean)',"%4.2f")
	qui distinct cityid if e(sample)==1
	estadd local N_clust=`r(ndistinct)'
	estadd local nomarg  
	estadd local noveri 
	local mlab `" `mlab' "(`i') Robust SEs" "'
	local ++i
	local c `c' c

	eststo r`i': reg adopted treatmenteffect_maxt_sig treatmenteffect_max staff_same_dpt pop_abovemed WWC_cert comm_preexisted mech_personalmotivation mech_simplification mech_socialcues if !(adopted==0 & adopted_marginal==1), cl(cityid)
	qui sum adopted if e(sample)==1
	estadd local avg=string(`r(mean)',"%4.2f")
	estadd local nomarg "$\checkmark$"
	estadd local noveri 
	local mlab `mlab' "(`i')"
	local ++i
	local c `c' c

	eststo r`i': reg adopted treatmenteffect_maxt_sig treatmenteffect_max staff_same_dpt pop_abovemed WWC_cert comm_preexisted mech_personalmotivation mech_simplification mech_socialcues if !(adopted==1 & adopted_strictdefn==0), cl(cityid)
	qui sum adopted if e(sample)==1
	estadd local avg=string(`r(mean)',"%4.2f")
	estadd local nomarg 
	estadd local noveri "$\checkmark$"
	local mlab `mlab' "(`i')"
	local ++i
	local c `c' c

	eststo r`i': reg adopted treatmenteffect_maxt_sig treatmenteffect_max staff_same_dpt pop_abovemed WWC_cert comm_preexisted mech_personalmotivation mech_simplification mech_socialcues if !(adopted==1 & adopted_strictdefn==0) & !(adopted==0 & adopted_marginal==1), cl(cityid)
	qui sum adopted if e(sample)==1
	estadd local avg=string(`r(mean)',"%4.2f")
	estadd local nomarg "$\checkmark$"
	estadd local noveri "$\checkmark$"
	local mlab `mlab' "(`i')"
	local ++i
	local c `c' c

	esttab r* using "tables/appendix/reg_robust.tex", ///
		substitute(\_ _) ///
		label se nostar nogaps nonotes nonumbers b(%4.2f) /// 
		mlabels("(1) " "(2)" "(3)" "(4)" "(5)", lhs("Dep. Var.: Nudge adopted (0/1, OLS)")) ///
		keep(treatmenteffect_maxt_sig treatmenteffect_max staff_same_dpt pop_abovemed WWC_cert comm_preexisted  mech_personalmotivation mech_simplification mech_socialcues _cons) ///
		order(treatmenteffect_maxt_sig treatmenteffect_max staff_same_dpt pop_abovemed WWC_cert comm_preexisted mech_simplification mech_personalmotivation mech_socialcues _cons) ///
		varlabels(	treatmenteffect_maxt_sig "Max $ t\geq1.96$" ///
					treatmenteffect_max "Max treatment effect (10pp.)" ///
					staff_same_dpt "City staff retained" ///
					pop_abovemed "Above-median city population" ///
					WWC_cert "What Works Cities certified" ///
					comm_preexisted "Communication pre-existed" ///
					mech_simplification "\hspace{1em}Simplification \& information" ///
					mech_personalmotivation "\hspace{1em}Personal motivation" ///				
					mech_socialcues "\hspace{1em}Social cues" ///
					_cons "Constant" ///
				) ///
		refcat(mech_simplification "\textit{Mechanism} \\ %" ///
				) ///
		stat(avg nomarg noveri N N_clust r2, fmt(%4.2f 0 0 0 0 %4.2f) ///
			label("Average adoption rate" "Dropping marginal non-adopts" "Dropping verbal-only adopts" "Number of trials" "Number of cities" "$ R^2$")) ///
		prehead(\begin{table}[htbp!] \centering \begin{threeparttable} ///
				\begin{tabular} {l `c'} \toprule ///
				\caption{Determinants of nudge adoption (robustness)\label{tab:reg_robust} } ///
				& Baseline & Robust SEs & \multicolumn{3}{c}{Robustness to marginal cases} \\ \cmidrule(lr){2-2} \cmidrule(lr){3-3} \cmidrule(lr){4-6} ///
				) ///
		postfoot(\bottomrule \end{tabular} ///
			\begin{tablenotes} \footnotesize ///
			\item Standard errors (shown in parentheses) are clustered at the city level except in Column 2, ///
					which provides heteroskedastic-robust standard errors. ///
				Baseline replicates Column 4 of Table 2. ///
				See Appendix D for details on marginal non-adoption and verbal-only adoption cases. ///
			\end{tablenotes} \end{threeparttable} \end{table}) ///
		replace
}

***************************************************************
***TABLE A.7: SAMPLE CHARACTERISTICS: SURVEY OF NON-ADOPTERS***
***************************************************************
{ 
	clear all 
	use "data/Survey.dta"
	keep if BIT==1
	bysort trialnumber: gen n=_n
	keep if n==1
	keep trialnumber
	gen byte survey_responded=1
	tempfile x
	save `x', replace

	use "data/BITTrialAdoption.dta"
	keep if sample==1
	merge 1:1 trialnumber using `x', nogen assert(1 3)

	gen byte survey_sent=((treatmenteffect_max>=1 | treatmenteffect_maxt>=1.96) & adopted==0)

	qui count if survey_sent==1
	qui assert `r(N)'==31
	qui count if survey_responded==1 & survey_sent==0
	qui assert `r(N)'==0

	keep if survey_sent==1
	replace survey_responded=0 if survey_responded==.

	local varlist 	treatmenteffect_maxt_sig treatmenteffect_max_ge1 ///
					WWC_cert staff_same_dpt department_imp staff_senior ///
					comm_preexisted mech_simplification mech_personalmotivation mech_socialcues ///
					policyarea_reve policyarea_regi policyarea_work policyarea_bene policyarea_comm policyarea_hlth policyarea_envi ///
					medium_physicalletter medium_email medium_postcard medium_text medium_website
					
	local insertendrow 	1 "\multicolumn{4}{l}{\textit{Nudge effectiveness}} \\ %" ///
						3 "\\ \multicolumn{4}{l}{\textit{Organizational features}} \\ %" ///
						7 "\\ \multicolumn{4}{l}{\textit{Experimental design}} \\ %" ///
						11 "\\ \multicolumn{4}{l}{\textit{Policy area}} \\ %" ///
						18 "\\ \multicolumn{4}{l}{\textit{Medium}} \\ %" ///
						23 "\hline"
	local rows: word count `varlist'
					
	mata: sumstats=J(`rows'+1,4,"")
	local r=1
	mata: sumstats[`r',1]="\hspace{1em}Max $ t{\geq}1.96$"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Max treatment effect $\geq 1$ pp."
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}City certified by What Works Cities"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}City staff member from trial retained"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Partner city dept. in charge of implementing"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Senior city staff on trial (Director/Chief)"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Communication pre-existed before trial"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Nudge communication uses Simplification"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Nudge communication uses Personal Motivation"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Nudge communication uses Social Cues"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Revenue collection \& debt repayment"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Registration \& regulation compliance"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Workforce \& education"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Take-up of benefits and programs"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Community engagement"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Health"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Environment"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Physical letter"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Email"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Postcard"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Text message"
	local ++r
	mata: sumstats[`r',1]="\hspace{1em}Website"
	local ++r
	mata: sumstats[`r',1]="Number of trials"
	local ++r

	qui count if trialnumber!=.
	mata: sumstats[`rows'+1,2]="`r(N)'"

	local catlist survey_responded
	local r=1
	foreach var in `varlist' {
		qui sum `var'
		local freq=string(`r(mean)'*100,"%3.2f")
		mata: sumstats[`r',2]="`freq'"
		
		local col=3
		
		forvalues x=0/1 {
			qui count if survey_responded==`x'
			mata: sumstats[`rows'+1,`col']="`r(N)'"
		
			qui sum `var' if survey_responded==`x'
			local freq=string(`r(mean)'*100,"%3.2f")
			mata: sumstats[`r',`col']="`freq'"
			
			if `x'==1 {
				reg `var' 0.survey_responded, cl(city)
				local pval=2*ttail(e(df_r),abs(_b[0.survey_responded]/_se[0.survey_responded]))
				tab `var' survey_responded, matcell(x) exact
				if `r(r)'>1 {
					local fisherp=`r(p_exact)'
					mata: x=st_matrix("x")
					mata: minfreq=min(x)
					mata: st_local("minfreq", strofreal(minfreq))
					if (`minfreq' < 5 & `fisherp'<0.05) | (`minfreq' >= 5 & `pval'<0.05) {
					mata: sumstats[`r',`col']="`freq'*"
				}
				}
				else {
					mata: sumstats[`r',`col']="`freq'"
				}
			}
			local ++col
		}
		local ++r
	}

	mmat2tex sumstats using "tables/appendix/sumstats_survey.tex", /// 
			replace ///
			preheader( ///
				\begin{table}[htbp!] \centering \begin{threeparttable} ///
				\caption{Sample characteristics: Survey of non-adopters} ///
				\begin{tabular} {l c c c} ///
				\toprule ///
				& Overall & \multicolumn{2}{c}{Responded}\\ ///
				\cmidrule(lr){2-2} \cmidrule(lr){3-4} ///
				Frequency in category (\%) & (1) & (2) No & (3) Yes \\ ///
				\hline ///
			) ///
			insertendrow(`insertendrow') ///
			bottom(\bottomrule \end{tabular} ///
				\label{tab:sumstats_survey} ///
				\begin{tablenotes} \footnotesize ///
					\item This table shows the frequencies of trials for each category listed in the leftmost column. Column 1 shows the frequences for all trials that had a treatment effect size $\ge1$ pp. or $ t\text{-stat}>1.96$ but were not adopted by the city. Columns 2 and 3 show the frequencies separately for cases when the did or did not city respond to the survey for Figure 10. ///
					\item *Asterisk indicates that the $ p\text{-value}$ of the difference{$<$}0.05. Standard errors are clustered by city. Except when there are fewer than 5 trials in one of the $ 2{\times}2$ cells, $ p\text{-values}$ are calculated using the two-sided Fisher's exact test instead. ///
				\end{tablenotes} \end{threeparttable} \end{table} ///
			)
}

*****************************************************************
***TABLE A.8a: HJORT ET AL. (2021) CONTROL AND TREATMENT RATES***
*****************************************************************
{ 
	clear all
	import delimited "data/policyadoption_raw.csv"

	* Generate additional adoption categories
	egen implemented1_sum=rowtotal(implemented1_ontime implemented1_audits implemented1_social implemented1_before implemented1_letter) if implemented1!=.
	gen byte implemented1_allmech=(implemented1_ontime + implemented1_audits + implemented1_social == 3) if implemented1_sum!=.
	label var implemented1_allmech "Adopted all 3 mechanisms (ontime, audits, social)"
	gen byte implemented1_2of3 = (implemented1_ontime + implemented1_audits + implemented1_social >=2) if implemented1_sum!=.
	label var implemented1_2of3 "Adopted at least 2 of ontime, audits/fine, and social norms"

	bysort munic_id: gen n=_n
	* Take MAX of adoption measures (since both mayor and city staff included; sometimes non-agreement, so take max)
	foreach var in implemented1_allmech implemented1_2of3 {
		egen `var'_max=max(`var'), by(munic_id)
		replace `var'_max=. if n!=1 // keep only one obs per city
	}

	mata	
		sum=J(4,7,"")
		sum[1,1]="No Letter + No Nudge"
		sum[2,1]="Letter + No Nudge"
		sum[3,1]="No Letter + Nudge"
		sum[4,1]="Letter + Nudge"
	end

	local col=2
	foreach nudge in implemented1_allmech implemented1_2of3 implemented1_social {
		
		*Control group moments
		qui tab `nudge' implemented1_letter if treatment_assignment==0, matcell(Pc)
		local Nc_nn=Pc[1,1]
		local Nc_ln=Pc[1,2]
		local Nc_nd=Pc[2,1]
		local Nc_ld=Pc[2,2]
		mat Pc=Pc/`r(N)'
		local Pc_nn=Pc[1,1]
		local Pc_ln=Pc[1,2]
		local Pc_nd=Pc[2,1]
		local Pc_ld=Pc[2,2]

		*Treatment group moments
		qui tab `nudge' implemented1_letter if treatment_assignment==1, matcell(Pt) 
		local Nt_nn=Pt[1,1]
		local Nt_ln=Pt[1,2]
		local Nt_nd=Pt[2,1]
		local Nt_ld=Pt[2,2]
		mat Pt=Pt/`r(N)'
		local Pt_nn=Pt[1,1]
		local Pt_ln=Pt[1,2]
		local Pt_nd=Pt[2,1]
		local Pt_ld=Pt[2,2]
		
		foreach g in c t {
			local row=1
			foreach c in nn ln nd ld {
				local P`g'_`c's=string(100*`P`g'_`c'',"%3.2f")
				mata: sum[`row',`col']="`P`g'_`c's'\% (`N`g'_`c'')"
				local ++row
			}
			local ++col
		}
	}

	mmat2tex sum using "tables/appendix/hjort_rates.tex", /// 
		replace ///
		preheader(\begin{table}[htbp!] \small \centering \begin{threeparttable} ///
			\caption{Hjort et al. (2021) policy adoption experiment: Control and treatment rates by adoption definition} ///
			\begin{tabular} {l c c c c c c} \toprule /// 
			\textit{Adoption definition} & \multicolumn{2}{c}{All 3 mechanisms} & \multicolumn{2}{c}{$ \geq2$ of 3 mechanisms} & \multicolumn{2}{c}{Social cues} \\ ///
			\cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(lr){6-7} ///
			\% ($ N$) & Control & Treatment & Control & Treatment & Control & Treatment \\ ///
			\hline ///
			) ///
		bottom( ///
			\bottomrule \end{tabular} ///
			\label{tab:hjort_rates} ///
			\begin{tablenotes} \footnotesize ///
			\item This table shows the frequency and number of mayors and city staff stating whether their city ///
					sends a tax payment reminder communication and what language it contains. ///
					The 3 mechanisms mentioned in the template for the tax reminder letter are ///
					the due date, the threat of audits or fines, and social norm language. ///
			\end{tablenotes} \end{threeparttable} \end{table})
}

****************************************************************
***TABLE A.8b: HJORT ET AL. (2021) BALANCE IN LETTER ADOPTION***
****************************************************************
{ // THIS REQUIRES CONFIDENTIAL MERGED MUNIC + MAYOR DATA SENT BY AUTHORS
	/*
	* Code is tailored from Hjort et al. (2021) code
	clear all
	use "data/policyexperiment_municIDs_char.dta"
	
	drop if treatment_assignment==1
	bysort munic_id: gen n=_n
	egen implemented1_letter_min = min(implemented1_letter), by(munic_id)
	egen implemented1_letter_max = max(implemented1_letter), by(munic_id)
	drop if implemented1_letter_min!=implemented1_letter_max // keep only consistent cases

	by cod_ibge, sort: egen endline_any=max(endline)
	gen attrition_munic=0 if endline_any==1
	replace attrition_munic=1 if endline_any==0
	replace attrition_munic=attrition_munic*100

	gen attrition=0 if endline==1
	replace attrition=1 if endline==0
	replace attrition=attrition*100
	replace male_mayor=male_mayor*100
	replace college_mayor=college_mayor*100
	replace mandate2_mayor=mandate2_mayor*100
	replace vtmargin_mayor=vtmargin_mayor*100
	replace party_left_mayor=party_left_mayor*100
	replace popul_2016= popul_2016/1000 // population in thousands
	// we replace public administration college with year t-1 if year t is missing
	replace share_college_pubadm2016= share_college_pubadm2016*100
	replace share_college_pubadm2016= share_college_pubadm2015*100 if share_college_pubadm2016==.
	replace share_college_pubadm2016= share_college_pubadm2014*100 if share_college_pubadm2016==.&share_college_pubadm2015==.
	replace share_college_pubadm2016= share_college_pubadm2013*100 if share_college_pubadm2016==.&share_college_pubadm2015==.&share_college_pubadm2014==.
	replace share_college_pubadm2016= share_college_pubadm2012*100 if share_college_pubadm2016==.&share_college_pubadm2015==.&share_college_pubadm2014==.&share_college_pubadm2013==.
	replace gini_index=gini_index*100
	replace big_south=big_south*100
	replace share_taxes_2010_2015=share_taxes_2010_2015*100

	// we collapse the data given that treatment assignment was done at municipality level (there are 2 observations per municipality: mayor and finance staff)
	collapse (mean) male_mayor age_mayor college_mayor mandate2_mayor vtmargin_mayor party_left_mayor ///
	popul_2016 college_population share_college_pubadm2016 poverty gini_index big_south income_pc_monthly share_taxes_2010_2015 ///
	treatment_assignment attended (max) implemented1_letter implemented3 endline, by(cod_ibge)
	
	qui logit implemented3 male_mayor age_mayor college_mayor mandate2_mayor vtmargin_mayor party_left_mayor popul_2016 college_population share_college_pubadm2016 poverty gini_index big_south income_pc_monthly share_taxes_2010_2015 if treatment_assignment==0
	predict implemented3_pred
	
	global vars male_mayor age_mayor college_mayor mandate2_mayor vtmargin_mayor party_left_mayor ///
	popul_2016 college_population share_college_pubadm2016 poverty gini_index big_south income_pc_monthly share_taxes_2010_2015 ///
	implemented3 implemented3_pred
	local lablist `" "Male" "Age" "College or more" "2nd Term" "Electoral Margin Victory" "Leftist Political Party" "Population" "College Population" "Public Adm College" "Poverty" "Gini" "Big South" "Per Capita Income" "Local Taxes Revenue (2010-15)" "Use of E-Procurement" "$ P(\text{Use of E-Procurement}|X) $" "'
	local rows: word count ${vars}
	local lastrow=`rows'+2
	local lastrowmin1=`lastrow'-1
	local lastrowmin2=`rows'
	
	* balance at CONTROL
	mat A=J(`rows'+2,3,.)
	mata: A=J(`rows'+2,4,"")
	local j=1

	foreach var in $vars {
		local lab: word `j' of `lablist'
		mata: A[`j',1]="\hspace{1em}`lab'"
		
		local lbl : variable label `var'
		dis "`lbl'"

		reg `var' implemented1_letter if treatment_assignment==0 & attended==0, r
		mat A[`j',1]=_b[_cons] // control group mean
		local x=string(_b[_cons],"%4.2f")
		mata: A[`j',1+1]="`x'"
		mat A[`j',2]=_b[implemented1_letter] // difference in mean treatment group
		local x=string(_b[implemented1_letter],"%4.2f")
		mata: A[`j',2+1]="`x'"
		mat A[`j',3]=(2 * ttail(e(df_r), abs(_b[implemented1_letter]/_se[implemented1_letter]))) // p-value of difference in mean
		local x=string((2 * ttail(e(df_r), abs(_b[implemented1_letter]/_se[implemented1_letter]))),"%4.2f")
		mata: A[`j',3+1]="`x'"
		local j=`j'+1
	}

	// F-test
	reg  implemented1_letter $vars if treatment_assignment==0 & attended==0, r
	test (male_mayor=0) (age_mayor=0) (college_mayor=0) (mandate2_mayor=0)(vtmargin_mayor=0)(party_left_mayor=0) ///
	(popul_2016=0)(college_population=0) (share_college_pubadm2016=0) ///
	(poverty=0)(gini_index=0)(big_south=0)(income_pc_monthly=0)(share_taxes_2010_2015=0) ///
	(implemented3=0) (implemented3_pred=0)
	mat A[`j',3]=	`r(p)' // p-value of F-test
	local x=string(`r(p)',"%4.2f")
	mata: A[`j',3+1]="`x'"
	mata: A[`j',1]="Joint $ F\text{-test}$"

	tab implemented1_letter if implemented1_letter==0 & treatment_assignment==0 & attended==0
	mat A[`j'+1,1]=	`r(N)' // number of control municipalities
	local x=string(`r(N)',"%4.0f")
	mata: A[`j'+1,1+1]="`x'"
	tab implemented1_letter if implemented1_letter==1 & treatment_assignment==0 & attended==0
	mat A[`j'+1,2]=	`r(N)' // number of treatment municipalities
	local x=string(`r(N)',"%4.0f")
	mata: A[`j'+1,2+1]="`x'"
	mata: A[`j'+1,1]="$ N $"

	matrix list A // balance in CONTROL

	mmat2tex A using "tables/appendix/hjort_balance.tex", /// 
			replace ///
			preheader(\begin{table}[htbp!] \centering \begin{threeparttable} ///
			\caption{Hjort et al. (2021) policy adoption experiment: Balance in letter adoption (control group)} ///
			\begin{tabular} {l c c c} \toprule /// 
			& Avg. in No Letter Group & {$\Delta$} in Letter Group & $ p\text{-value}$ \\ \hline ///
					) ///
			insertendrow(1 "\textit{Mayor's Characteristics} \\ " ///
						7 "\\ \textit{Municipalities' Characteristics} \\" ///
						15 "\\ \textit{Placebo Adoption} \\" ///
						`lastrowmin1' "\\" ///
						`lastrow' "\hline") ///
			bottom(\bottomrule \end{tabular} ///
			\label{tab:hjort_balance} ///
			\begin{tablenotes} \footnotesize ///
			\item This table compares observable charactertistics between the municipalities in the Control Group from Hjort et al. (2021) ///
				that were not sending a taxpayer reminder letter with those that were. ///
				Municipalities in which the mayor and the city staff gave conflicting responses to letter adoption are excluded. ///
				$\Delta$ in Letter Group is the difference in the means from the No Letter Group to the Letter Group. ///
				The $ p\text{-value}$ is computed from robust standard errors. ///
				The Use of E-Procurement is considered as a placebo adoption outcome in Hjort et al. (2021). ///
				$ P(\text{Use of E-Procurement}|X)$ is the predicted probability of adopting E-Procurement based on ///
					a logit model including all listed Mayor and Municipalities' characteristics. ///
					The Joint $ F\text{-test}$ includes all variables in the table. ///
			\end{tablenotes} \end{threeparttable} \end{table})
	*/
}